package netty.coffee;

import io.netty.channel.Channel;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Author zhangtao
 * @create 2021/12/16 17:54
 */
public class CoffeeTask implements Runnable {
    private static final Logger log = LoggerFactory.getLogger(CoffeeTask.class);
    String data = "时间;164737;P;productId;;1;171;75;61;680;1\u00033F";
    String endData = "时间;164737;E;1;1\u000316";
    Map<String, Integer> productMap = new HashMap<>();
    ThreadLocalRandom random = ThreadLocalRandom.current();
    String productAdd = "";
    String productId = "";
    int count = 0;
    int endIndex = 0;
    Channel channel;

    CoffeeTask(Channel channel) {
        this.channel = channel;
    }

    @Override
    public void run() {
        try {
            endIndex++;
            String[] coffeeData = data.split(";");
            coffeeData[0] = DateTime.now().toString("yyyy-MM-dd'T'HH:mm:ss");
            if ("D".equals(productAdd)) {
                coffeeData[3] = productId;
                productAdd = "";
                coffeeData[4] = "";
                Integer counter = productMap.getOrDefault(productId, 0);
                counter = counter + 2;
                productMap.put(productId, counter);
                coffeeData[10] = counter + "\u00033F";
            } else {
//                int r = 7;//random.nextInt(2, 3);
//                if (r == 5) {
//                    productId = "10";
//                } else {
//                    productId = String.valueOf(r);
//                }
                productId = "9";
                coffeeData[3] = productId;
                Integer counter = productMap.getOrDefault(productId, 0);
                if ("2".equals(productId)) {
                    productAdd = "D";
                    coffeeData[4] = "D";
                    counter = counter + 2;
                } else {
                    counter = counter + 1;
                }

                productMap.put(productId, counter);
                coffeeData[10] = counter + "\u00033F";
                coffeeData[6] = String.valueOf(random.nextInt(50, 250));
            }
            if ("10".equals(productId)) {
                coffeeData[7] = "";
                coffeeData[8] = "";
                coffeeData[9] = "";
            }
            String response = StringUtils.joinWith(";", coffeeData);
            log.info(response);
            // 发送客户端的请求
            channel.writeAndFlush(response);
//            if (!"D".equals(productAdd) && !"10".equals(productAdd)) {
//                count++;
//                channel.writeAndFlush("2021-11-29T17:54:18;164737;E;1;1\u000316");
//                                log.info("2021-11-29T17:54:18;164737;E;1;1\u000316");
//            }
            if (endIndex % 3 == 0) {
                String e1 = endData.replace("时间", DateTime.now().toString("yyyy-MM-dd'T'HH:mm:ss"));
                log.info(e1);
                channel.writeAndFlush(e1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
